package com.itmyx.base.controller;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.itmyx.base.util.UuidUtil;
import com.itmyx.base.util.data.ParamData;
import com.itmyx.base.util.date.GetDateUtil;
import com.itmyx.base.util.log.Logger;

/**
 * 基础Controller 供继承
 * @author myx
 *
 */
public class BaseController {
	
	protected Logger logger = Logger.getLogger(Logger.class);
	
	protected HttpServletRequest request;  

    protected HttpServletResponse response;  

    protected HttpSession session;  


    /**
     * 设置对象   解决跨域请求问题
     * @param request
     * @param response
     */
    @ModelAttribute  
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){  
        this.request = request;  
        this.response = response;  
        response.setHeader("Access-Control-Allow-Origin", "*");
        this.session = request.getSession();  

    }  
	
	/**
	 * 得到PageData
	 */
	public ParamData getPageData(){
		return new ParamData(this.getRequest());
	}
	
	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**
	 * 得到32位的uuid
	 * @return
	 */
	public String get32UUID(){
		return UuidUtil.get32UUID();
	}
	
	/**
	 * 日志记录开始显示访问哪个接口
	 * @param param 
	 * @param string 
	 */
	public static void logBefore(Logger logger, String msg, String interfaceName, ParamData param){
		logger.info("");
		logger.info("--------------------"+GetDateUtil.getTime(new Date())+"-------------------------------------");
		logger.info("接口名称:"+interfaceName);
		logger.info("参数列表:"+param);
	}
	
	/**
	 * 日志记录结束显示返回什么信息
	 */
	public static void logAfter(Logger logger,String result){
		logger.info("返回结果:"+result);
		logger.info("--------------------"+GetDateUtil.getTime(new Date())+"-------------------------------------");
		logger.info("");
	}
	
}
